home *** CD-ROM | disk | FTP | other *** search
/ PC World 2008 September / PCWorld_2008-09_cd.bin / v cisle / sadanastroju / wot-20080519-fx.xpi / chrome / wot.jar / content / my.js < prev    next >
Text File  |  2008-05-08  |  3KB  |  142 lines

  1. /*
  2.     my.js
  3.  
  4.     Copyright ┬⌐ 2005, 2006, 2007, 2008  Against Intuition, Inc. <info@mywot.com>
  5. */
  6.  
  7. var wot_my_session =
  8. {
  9.     init: function()
  10.     {
  11.         try {
  12.             if (this.uri && this.cs) {
  13.                 return;
  14.             }
  15.  
  16.             var ios = Components.classes["@mozilla.org/network/io-service;1"].
  17.                         getService(Components.interfaces.nsIIOService);
  18.  
  19.             this.uri = ios.newURI("http://" + WOT_MY_COOKIE_DOMAIN + "/",
  20.                             "", null);
  21.  
  22.             this.domain = "; domain=" + WOT_MY_COOKIE_DOMAIN;
  23.  
  24.             this.cs = Components.classes["@mozilla.org/cookieService;1"].
  25.                         getService(Components.interfaces.nsICookieService);
  26.  
  27.             window.addEventListener("unload", function(e) {
  28.                     wot_my_session.unload();
  29.                 }, false);
  30.         } catch (e) {
  31.             dump("wot_my_session.init: failed with " + e + "\n");
  32.         }
  33.     },
  34.  
  35.     unload: function()
  36.     {
  37.         this.ios = null;
  38.         this.uri = null;
  39.         this.cs = null;
  40.     },
  41.  
  42.     clear: function()
  43.     {
  44.         try {
  45.             var mgr = Components.classes["@mozilla.org/cookiemanager;1"].
  46.                         getService(Components.interfaces.nsICookieManager);
  47.             if (mgr) {
  48.                 mgr.remove(WOT_MY_COOKIE_DOMAIN, "id", "/", false);
  49.                 mgr.remove(WOT_MY_COOKIE_DOMAIN, "nonce", "/", false);
  50.                 mgr.remove(WOT_MY_COOKIE_DOMAIN, "auth", "/", false);
  51.                 mgr.remove(WOT_MY_COOKIE_DOMAIN, "authid", "/", false);
  52.                 mgr.remove(WOT_MY_COOKIE_DOMAIN, "reload", "/", false);
  53.             }
  54.         } catch (e) {
  55.             dump("wot_my_session.clear: failed with " + e + "\n");
  56.         }
  57.     },
  58.  
  59.     update: function(force)
  60.     {
  61.         try {
  62.             if (!wot_api_register.ready || !this.uri || !this.cs) {
  63.                 return;
  64.             }
  65.  
  66.             this.cs.setCookieString(this.uri, null, "accessible=" +
  67.                 wot_prefs.accessible + this.domain, null);
  68.  
  69.             if (!wot_prefs.my_cookies) {
  70.                 if (force) {
  71.                     this.clear();
  72.                 }
  73.                 return;
  74.             }
  75.  
  76.             /* If it has been WOT_MY_SESSION_LENGTH seconds since the
  77.                 session was last updated, force an update */
  78.             if ((Date.now() - Number(wot_prefs.cookie_updated)) >
  79.                     WOT_MY_SESSION_LENGTH) {
  80.                 force = true;
  81.             }
  82.  
  83.             /* If we have an authid cookie set by the server (for any id),
  84.                 don't update unless forced */
  85.             var authid = new RegExp("authid=\\w{" +
  86.                             WOT_LENGTH_WITNESS_ID + "}");
  87.             var current = this.cs.getCookieString(this.uri, null);
  88.  
  89.             if (!force && current && current.match(authid)) {
  90.                 return;
  91.             }
  92.  
  93.             /* Update authentication cookies */
  94.             var id = "id=" + wot_prefs.witness_id;
  95.             this.cs.setCookieString(this.uri, null, id + this.domain, null);
  96.  
  97.             var nonce = "nonce=" + wot_crypto.nonce();
  98.             this.cs.setCookieString(this.uri, null, nonce + this.domain, null);
  99.  
  100.             var auth = "auth=" +
  101.                 wot_crypto.authenticate(id + "&" + nonce);
  102.             this.cs.setCookieString(this.uri, null, auth + this.domain, null);
  103.  
  104.             /* Update time */
  105.             wot_prefs.setChar("cookie_updated", Date.now().toString());
  106.         } catch (e) {
  107.             dump("wot_my_session.update: failed with " + e + "\n");
  108.         }
  109.     },
  110.  
  111.     reload: function()
  112.     {
  113.         try {
  114.             if (!wot_api_register.ready || !wot_prefs.my_cookies) {
  115.                 return;
  116.             }
  117.  
  118.             var current = this.cs.getCookieString(this.uri, null);
  119.  
  120.             if (!current) {
  121.                 return;
  122.             }
  123.  
  124.             var reload = new RegExp("reload=(\\w{" +
  125.                             WOT_LENGTH_WITNESS_ID + "})");
  126.  
  127.             var match = current.match(reload);
  128.  
  129.             /* Reload if we have a reload cookie, but with a different id than
  130.                 ours */
  131.             if (match && match[1] &&
  132.                     match[1] != wot_prefs.witness_id) {
  133.                 wot_api_reload.send(match[1]);
  134.             }
  135.         } catch (e) {
  136.             dump("wot_my_session.reload: failed with " + e + "\n");
  137.         }
  138.     }
  139. };
  140.  
  141. wot_my_session.init();
  142.